// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <nebula/array/array_decimal.h>

#include <cstdint>
#include <memory>
#include <string>

#include <nebula/array/array_binary.h>
#include <nebula/array/data.h>
#include <nebula/types/type.h>
#include <turbo/base/checked_cast.h>
#include <nebula/numeric/decimal.h>
#include <turbo/log/logging.h>

namespace nebula {


    // ----------------------------------------------------------------------
    // Decimal128

    Decimal128Array::Decimal128Array(const std::shared_ptr<ArrayData> &data)
            : FixedSizeBinaryArray(data) {
        KCHECK_EQ(data->type->id(), Type::DECIMAL128);
    }

    std::string Decimal128Array::format_value(int64_t i) const {
        const auto &type_ = turbo::checked_cast<const Decimal128Type &>(*type());
        const Decimal128 value(get_value(i));
        return value.to_string(type_.scale());
    }

    // ----------------------------------------------------------------------
    // Decimal256

    Decimal256Array::Decimal256Array(const std::shared_ptr<ArrayData> &data)
            : FixedSizeBinaryArray(data) {
        KCHECK_EQ(data->type->id(), Type::DECIMAL256);
    }

    std::string Decimal256Array::format_value(int64_t i) const {
        const auto &type_ = turbo::checked_cast<const Decimal256Type &>(*type());
        const Decimal256 value(get_value(i));
        return value.to_string(type_.scale());
    }

}  // namespace nebula
